home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / spwno413.zip / SWAPTEST.C < prev    next >
C/C++ Source or Header  |  1991-11-16  |  7KB  |  249 lines

  1. /********************************************************************/
  2. /*   SPAWNO v4.0   EMS/XMS/disk swapping replacement for spawn...() */
  3. /*   (c) Copyright 1991 Ralf Brown  All Rights Reserved            */
  4. /*                                    */
  5. /*   This file SWAPTEST.C is donated to the public domain, but the  */
  6. /*   remainder of SPAWNO remains copyrighted.                */
  7. /*                                    */
  8. /*   Changes for MSC compatibility provided by Gene McManus.        */
  9. /********************************************************************/
  10.  
  11. /*  Available #defines:
  12.     NO_SPAWNO    assume file will be linked only with runtime library
  13.     STD_SPAWN    use replacements for standard functions from SPAWNO lib
  14.     ENVIRON        pass current environment rather than original environment
  15.     ADD_MEM        allocate ADD_MEM KB additional memory to SWAPTEST
  16.  
  17.     Recompiling:
  18.     TCC -m{x} SWAPTEST SPAWN{x}.LIB
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <dos.h>
  24. #include <ctype.h>
  25. #ifndef M_I86
  26. #include <alloc.h>
  27. #endif /* M_I86 */
  28. #include <process.h>
  29. #include "spawno.h"
  30.  
  31. /* #define STD_SPAWN       /* comment out to use parallel spawn..o() funcs */
  32. /* #define NO_SPAWNO       /* uncomment to use compiler's library */
  33. /* #define ADD_MEM 300       /* uncomment to allocate additional 300K memory */
  34.  
  35. #ifndef M_I86    /* allow for differences between TC & MSC */
  36. #define ALLOC allocmem
  37. #define FREE  freemem
  38. #define OK    -1
  39. #else
  40. #define ALLOC _dos_allocmem
  41. #define FREE  _dos_freemem
  42. #define OK    0
  43. #endif /* M_I86 */
  44.  
  45. #ifdef NO_SPAWNO
  46. #  ifndef STD_SPAWN
  47. #    define STD_SPAWN
  48. #  endif
  49. #endif
  50.  
  51. #ifdef STD_SPAWN
  52. #  ifdef ENVIRON
  53. #    define SPAWNlp spawnlpe
  54. #    define SPAWNvp spawnvpe
  55. #  else
  56. #    define SPAWNlp spawnlp
  57. #    define SPAWNvp spawnvp
  58. #  endif
  59. #  define SPAWN_ARG1 P_WAIT
  60. #else /* STD_SPAWN */
  61. #  ifdef ENVIRON
  62. #    define SPAWNlp spawnlpeo
  63. #    define SPAWNvp spawnvpeo
  64. #  else
  65. #    define SPAWNlp spawnlpo
  66. #    define SPAWNvp spawnvpo
  67. #  endif
  68. #  define SPAWN_ARG1 swap_dir
  69. #endif /* STD_SPAWN */
  70.  
  71. #ifndef ENVIRON
  72. unsigned int _heaplen = 128 ;    /* limit heap if we don't build an environment */
  73. #endif /* ENVIRON */
  74.  
  75. int verbose = 0 ;
  76.  
  77. void usage(void)
  78. {
  79.    fputs("Usage: SWAPTEST <runs> <where> <size> [command [args]]\n",stderr) ;
  80.    fputs("\twhere\t<runs> is the number of times to recursively invoke\n",stderr) ;
  81.    fputs("\t\t\tSWAPTEST before invoking the specified command.\n",stderr) ;
  82.    fputs("\t\t<where> indicates where to swap, one or more of [D]isk,\n",stderr) ;
  83.    fputs("\t\t\t[E]MS, [X]MS, and ex[T]ended\n",stderr) ;
  84.    fputs("\t\t<size> specifies the minimum number of bytes to keep resident.\n",stderr) ;
  85.    exit(1) ;
  86. }
  87.  
  88. #ifdef NO_SPAWNO
  89. void init_SPAWNO(const char *overlay_path,int swap_types)
  90. {
  91.    (void) overlay_path ;
  92.    (void) swap_types ;
  93. }
  94. #endif /* NO_SPAWNO */
  95.  
  96. int main(int argc, char **argv)
  97. {
  98.    unsigned int seg1, seg2, seg3, seg4 ;
  99.    unsigned int recursions_left ;
  100.    char *argptr ;
  101.    char *comspec = getenv("COMSPEC") ;
  102.    char *swap_dir = getenv("SWAPDIR") ;
  103.    int swap_types = 0 ;
  104.    char num[12] ;
  105.    int retval ;
  106. #ifdef ENVIRON
  107.    char *scratch ;
  108. #endif
  109.  
  110.    if (argc < 4)
  111.       usage() ;
  112. /***** process the commandline arguments *****/
  113.    if (argv[0] == NULL || argv[0][0] == '\0')
  114.       argv[0] = "SWAPTEST" ;
  115.    recursions_left = (unsigned int) atol(argv[1]) ;
  116.    argptr = argv[2] ;
  117.    while (*argptr)
  118.       {
  119.       switch(toupper(*argptr))
  120.      {
  121.      case 'V': verbose = 1 ;
  122.            break ;
  123.      case 'D': swap_types |= SWAP_DISK ;
  124.            break ;
  125.      case 'E': swap_types |= SWAP_EMS ;
  126.            break ;
  127.      case 'X': swap_types |= SWAP_XMS ;
  128.            break ;
  129.      case 'T': swap_types |= SWAP_EXT ;
  130.            break ;
  131.      default:  /* do nothing */
  132.            break ;
  133.      }
  134.       argptr++ ;
  135.       }
  136. #ifndef NO_SPAWNO
  137.    /* determine the number of paragraphs to keep resident given the number of */
  138.    /* bytes requested by the user */
  139.    __spawn_resident = ((unsigned int) atol(argv[3]) + 15) / 16 ;
  140. #endif /* ndef NO_SPAWNO */
  141.    if (swap_dir == NULL)
  142.       swap_dir = "." ;          /* default swap directory is current dir */
  143.    if (comspec == NULL)
  144.       comspec = "COMMAND" ;    /* default program to run is COMMAND.COM */
  145.    init_SPAWNO(swap_dir,swap_types) ;
  146. #ifdef ENVIRON
  147.    /* Turbo C's malloc() [called by putenv()] can't deal with other memory  */
  148.    /* blocks immediately beyond the main program's, so allocate some memory */
  149.    /* now to force the break higher */
  150.    scratch = malloc(2000) ;
  151. #endif /* ENVIRON */
  152. #ifdef ADD_MEM
  153.    {
  154.    int i, j ;
  155.    char *tmp ;
  156.  
  157.    if (verbose) printf("Allocating %dK additional memory\n",ADD_MEM) ;
  158.    for (i = 0 ; i < ADD_MEM ; i++)     /* allocate extra memory to increase */
  159.       {                       /* space needed for swapping */
  160.       tmp = malloc(1024) ;
  161.       for (j = 0 ; j < 1024 ; j++)     /* initialize memory to a known pattern */
  162.      tmp[j] = 1023 - j ;
  163.       }
  164.    }
  165. #endif /* ADD_MEM */
  166. /***** allocate several blocks of memory *****/
  167.    if (ALLOC(2048,&seg1) != OK)      /* 32K */
  168.       {
  169.       printf("First memory allocation failed.\n") ;
  170.       abort() ;
  171.       }
  172.    if (verbose) printf("seg1 = %4.4X\n",seg1) ;
  173.    if (ALLOC(64,&seg2) != OK)      /* 1K */
  174.       {
  175.       printf("Second memory allocation failed.\n") ;
  176.       abort() ;
  177.       }
  178.    if (verbose) printf("seg2 = %4.4X\n",seg2) ;
  179.    if (ALLOC(4100,&seg3) != OK)      /* 64+K */
  180.       {
  181.       printf("Third memory allocation failed.\n") ;
  182.       abort() ;
  183.       }
  184.    if (verbose) printf("seg3 = %4.4X\n",seg3) ;
  185.    if (ALLOC(128,&seg4) != OK)      /* 2K */
  186.       {
  187.       printf("Fourth memory allocation failed.\n") ;
  188.       abort() ;
  189.       }
  190.    if (verbose) printf("seg4 = %4.4X\n",seg4) ;
  191. /***** deallocate one of our blocks to create a "hole" *****/
  192.    FREE(seg2) ;
  193.    if (verbose) printf("freed seg2\n") ;
  194. #ifdef ENVIRON
  195.    free(scratch) ;
  196. #endif /* ENVIRON */
  197. /***** now figure out which program to spawn and spawn it *****/   
  198.    printf("SWAPTEST %u\n",recursions_left) ;
  199.    if (recursions_left)
  200.       {
  201.       ultoa(recursions_left-1,num,10) ;
  202.       argv[1] = num ;
  203. #ifdef STD_SPAWN
  204.       retval = spawnvp(P_WAIT,argv[0],argv) ;
  205. #else
  206.       retval = spawnvpo(swap_dir,argv[0],argv) ;
  207. #endif /* STD_SPAWN */
  208.       }
  209.    else
  210.       {
  211. #ifdef ENVIRON
  212.       putenv("SWAPTEST=YES") ;
  213. #endif /* ENVIRON */
  214.       if (argc > 4)
  215.      retval = SPAWNvp(SPAWN_ARG1,argv[4],argv+4
  216. #ifdef ENVIRON
  217.                          ,environ
  218. #endif /* ENVIRON */
  219.              ) ; 
  220.       else
  221.      retval = SPAWNlp(SPAWN_ARG1,comspec,comspec,(char *)NULL
  222. #ifdef ENVIRON
  223.                              ,environ
  224. #endif /* ENVIRON */
  225.              ) ;
  226.       }
  227.    if (retval == -1)
  228.       printf("errno = %d, _doserrno = %d.",errno,_doserrno) ;
  229.    else
  230.       printf("spawn return code = %d.",retval) ;
  231. /***** check memory integrity on return by freeing the *****/
  232. /***** memory we allocated earlier *****/
  233.    if (FREE(seg1))
  234.       printf("\nfreemem(seg1) failed!") ;
  235.    if (FREE(seg4))
  236.       printf("\nfreemem(seg4) failed!") ;
  237.    if (FREE(seg3))
  238.       printf("\nfreemem(seg3) failed!") ;
  239. /***** allocate one big block of memory *****/
  240.    if (ALLOC(6340,&seg1) != OK)
  241.       abort() ;
  242.    if (verbose) printf("\n6340 paragraphs allocated at %4.4X",seg1) ;
  243.    if (FREE(seg1))
  244.       printf("\nfinal freemem failed!") ;
  245.    printf("\t Ending SWAPTEST %u\n",recursions_left) ;
  246.    return 0 ;          /* successful */
  247. }
  248.  
  249.